package qing.controller.sys;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import qing.common.u.HttpSessionProvider;
import qing.common.u.usession;
import qing.controller.CommonController;
import qing.entity.Person;
import qing.service.PersonService;



@Controller
@RequestMapping("/api")
public class LoginController  {
	
	@Autowired
	private PersonService personService;

	//判断是否登录
	@RequestMapping(value="islogin",produces = "text/html;charset=UTF-8")  //produces 为防止中文乱码添加
	@ResponseBody
	public String cmsislogin(HttpServletRequest request) {
		 Map<String,Object> map=new HashMap<String,Object>();
		 Person person = personService.getPersonByAccount("whx");
		 HttpSession session=request.getSession();
		 session.setAttribute("user", person); 
		 session.setMaxInactiveInterval(30*60);
		 
		 map.put("status", 1);
		 map.put("msg","/cms/index");
			
		 
		/*Object user = request.getSession().getAttribute("user");
		if (user == null) {
			map.put("status", 0);
			map.put("msg","用户未登录");
		}else
		{
			map.put("status", 1);
			map.put("msg","/cms/index");
		}*/
		return JSON.toJSONString(map);
	}
	//后台登录操作
	@RequestMapping(value="login",produces = "text/html;charset=UTF-8",method=RequestMethod.POST)  //produces 为防止中文乱码添加
	@ResponseBody
	public String login(HttpServletRequest request,Person p) {
		Map<String,Object> map=new HashMap<String,Object>();
		usession usession=new HttpSessionProvider();
		if (p.getUsername().isEmpty() || p.getPassword().isEmpty() || p.getCode().isEmpty()) {
			map.put("status", 0);
			map.put("msg", "请填写用户名和密码"); 
			return JSON.toJSONString(map);
		}
		String scode = (String)usession.getAttribute(request, "verCode");
		if (!scode.equals(p.getCode().toLowerCase())) {
			map.put("status", 0);
			map.put("msg", "验证码错误，应为" +scode); 
			return JSON.toJSONString(map);
		}
		Person person = personService.getPerson(p.getUsername(), p.getPassword());
		if (person == null) {
			map.put("status", 0);
			map.put("msg", "用户名或密码错误"); 
			return JSON.toJSONString(map);
		}
		map.put("status", 1);
		map.put("msg","/admin/index");
		person.setDateLastActived(new Date());
		person.setIpLastActived(request.getRemoteAddr());
		usession.setAttribute(request, null, "user", person);
		personService.update(person);
		return JSON.toJSONString(map);
	}

	@RequestMapping("logon")
	public String logon(HttpSession session) {
		session.setAttribute("user", null);
		return "/admin/login.html";
	}
	


	
	

}
